﻿using DevExpress.XtraEditors;
using DevTeam;

using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.GomlekCekiListesi
{
    public partial class WB : XIPL
    {
        public WB()
        {
            InitializeComponent();

            Init("WB Çeki Listesi", false);
            this.FileDone += DestexTip1_FileDone;
        }

        private void WB_Load(object sender, EventArgs e)
        {

        }

        void DestexTip1_FileDone(DataSet ds, string FileName)
        {
            int PlTableCount = 0;
            foreach (DataTable dt in ds.Tables)
                if (dt.TableName.StartsWith("PL"))
                {
                    PlTableCount++;

                    DataRowCollection rows = dt.Rows;
                    DataColumnCollection cols = dt.Columns;
                    int orderLineStart = -1;

                    for (int i = 0; i < rows.Count; i++)
                    {
                        DataRow dr = rows[i];
                        if (dr[0].ToStr().ToUpper().Contains("BIG BOX NO"))
                        {
                            orderLineStart = i + 3;
                            break;
                        }
                    }

                    Dictionary<string, int> indexes = new Dictionary<string, int>();

                    for (int i = 1; i <= 3; i++)
                    {
                        DataRow dr0 = dt.Rows[orderLineStart - i];
                        for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                        {
                            switch (dr0[colIndex].ToStr())
                            {
                                case "BIG BOX NO": indexes.Add("BigBoxNo", colIndex); break;
                                case "BOX NO": indexes.Add("BoxNo", colIndex); break;
                                case "Style":
                                case "STYLE": indexes.Add("Model", colIndex); break;
                                case "Pos":
                                case "POS": indexes.Add("Artikel", colIndex); break;
                                case "Color": indexes.Add("Renk", colIndex); break;
                                case "Customer":
                                case "CUSTOMER": indexes.Add("Magaza", colIndex); break;
                                case "BRAND": indexes.Add("Brand", colIndex); break;
                                case "City": indexes.Add("City", colIndex); break;
                                case "SIZES": indexes.Add("SizesStart", colIndex); break;
                                case "TOT": indexes.Add("TOT", colIndex); break;
                                case "DosyaNo": indexes.Add("DosyaNo", colIndex); break;

                            }
                        }
                    }

                    if (!indexes.ContainsKey("DosyaNo"))
                    {
                        XtraMessageBox.Show(dt.TableName + " sheet'inde DosyaNo sütunu yok! Ekleyip tekrar deneyin.");
                        return;
                    }
                    if (!indexes.ContainsKey("SizesStart"))
                    {
                        XtraMessageBox.Show(dt.TableName + " sheet'inde beden dağılımının başladığını gösteren SIZES sütunu yok! Ekleyip tekrar deneyin.");
                        return;
                    }

                    int SizesEnd = indexes["SizesStart"];
                    if (indexes.ContainsKey("TOT"))
                        SizesEnd = indexes["TOT"];
                    else
                        SizesEnd = indexes["DosyaNo"];

                    int DagilimId = 0;
                    DataTable dagTable = null;
                    string DosyaNo = rows[orderLineStart][indexes["DosyaNo"]].ToStr();
                    if (DosyaNo != string.Empty)
                    {
                        DataSet d = ExecuteDataSet("GetDagilimByDosyaNo", DosyaNo);
                        DagilimId = d.Tables[0].Rows[0][0].ToInt();
                        dagTable = d.Tables[1];

                        if (DagilimId > 0)
                        {
                            for (int j = indexes["SizesStart"]; j < SizesEnd; j++)
                            {
                                string Cap = rows[orderLineStart - 1][j].ToStr();

                                DataRow[] r = dagTable.Select("Beden='" + Cap + "'", string.Empty);
                                if (r.Length > 0)
                                    dtBD.Rows.Add(Cap, r[0]["Beden"].ToStr(), r[0]["Name"].ToStr());
                                    //BD[Cap] = r[0]["Name"].ToStr().Replace("D", "A"); //D->A ları SP'lerde ve programda düzelt, hepsi A olsun!
                                else
                                    dtBD.Rows.Add(Cap, null, null);                                    
                            }

                            bool unresolved = false;
                            unresolved = dtBD.Compute("COUNT(Caption)", "Beden is null").ToInt() > 0;

                            if (unresolved)
                            {
                                using (BDMatch bdm = new BDMatch(dtBD, DagilimId))
                                {
                                    if (bdm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                                    {
                                        dtBD.Clear();
                                        dtBD = bdm.dt;
                                    }
                                    else
                                        return;

                                }
                            }
                        }
                        else
                        {
                            XtraMessageBox.Show("Planlamaya bu dosyanın dagilimId'si girilmemiş!");
                            return;
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show(dt.TableName + " sheet'inde ilk satıra DosyaNo girilmemiş!");
                        return;
                    }



                    Dictionary<string, string> BD = new Dictionary<string, string>();
                    foreach (DataRow r in dtBD.Rows)
                        BD[r["Caption"].ToStr()] = r["Name"].ToStr();

                    for (int i = orderLineStart; i < rows.Count; i++)
                    {
                        DataRow dr = rows[i];
                        DataRow drArticle = Article.NewRow();
                        foreach (KeyValuePair<string, int> idx in indexes)
                            if (Article.Columns.Contains(idx.Key))
                                drArticle[idx.Key] = dr[idx.Value];

                        if (DagilimId > 0)
                            for (int j = indexes["SizesStart"]; j < SizesEnd; j++)
                            {
                                string Cap = rows[orderLineStart - 1][j].ToStr();

                                if (dr[j].ToInt() > 0)
                                    if (BD[Cap]!=string.Empty)
                                    drArticle[BD[Cap]] = dr[j].ToInt();
                                    else
                                    {
                                        XtraMessageBox.Show(Cap + " bedeninin sistemdeki karşılığını girmemişsiniz!");
                                        return;
                                    }
                            }

                        Article.Rows.Add(drArticle);
                    }

                }

            if (PlTableCount == 0)
                XtraMessageBox.Show("Dosyada, adı PL ile başlayan sheet yok!");


            dtBoth = ExecuteDataTable("ComparePL", Article.Serialize());
            
        }
    }
}
